﻿Imports System.Threading
Imports System.Globalization




Public Class MessageWindow


    ' Query qui donne la liste de tous les GUID par défaut de l'écran (MetaApplicationOption / MetaUserOption) se fait dans la query seul...
    ''
    Const Query_LocalOption As String = "LocalOption"
    Const Query_AdminOption As String = "AdminOption"


    Public Sub New()
        Dim aCulture As String = "en-US"
        ' This call is required by the designer.
        ' Set the culture and UI culture before 
        ' the call to InitializeComponent.
        aCulture = New AppOption().Culture
        Thread.CurrentThread.CurrentCulture = New CultureInfo(aCulture)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(aCulture)

        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub




    Private Shared dataStream As System.IO.MemoryStream
    ''' <summary>
    ''' This class is use to display message
    ''' </summary>
    ''' <param name="msg">Message that will be display</param>
    ''' <param name="msgBoxStyle">OkCancel or MsgBoxStyle.Information when information show close button only</param>
    ''' <param name="logError">Save the message in a log file </param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ShowMessage(ByVal msg As String, Optional ByVal msgBoxStyle As MsgBoxStyle = MsgBoxStyle.OkCancel, Optional ByVal logError As Boolean = False) As DialogResult
        Dim frm As MessageWindow = New MessageWindow()
        CaptureScreen()
        frm.Memo.Text = msg
        frm.btnOk.Focus()
        frm.btnSendSupport.Hide()
        frm.meComment.Hide()
        frm.lbComment.Hide()
        frm.Height = 273
        If msgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.Information Then
            frm.btnOk.Hide()
        End If
        If logError Then
            'outstanding: log message somewhere in the database... aleblanc 
            'Module should be in commonext so we can reuse on the www...
        End If
        frm.Cursor = Cursors.Default
        Return frm.ShowDialog()
    End Function

    Public Shared Function ShowMessage(ByVal ex As Exception, Optional ByVal msgBoxStyle As MsgBoxStyle = MsgBoxStyle.OkCancel, Optional ByVal logError As Boolean = False) As DialogResult
        Dim frm As MessageWindow = New MessageWindow()
        CaptureScreen()
        frm.Memo.Text = String.Format("{0}{1}{2}{1}{3}{1}{4}", ex.Message, vbCrLf, ex.InnerException, ex.Source, ex.StackTrace)
        If msgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.Information Then
            frm.btnOk.Hide()
        End If
        If logError Then
            'outstanding: log message somewhere in the database... aleblanc 
            'Module should be in commonext so we can reuse on the www...
        End If
        frm.Cursor = Cursors.Default
        Return frm.ShowDialog()
    End Function

    Private Sub btnSendSupport_Click(sender As System.Object, e As System.EventArgs) Handles btnSendSupport.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim mail As CommonExt.Utils.EmailExt = New CommonExt.Utils.EmailExt(AppManager.SMTPServer)

            '
            ' Mettre en commentaire pour enlever le print screen de l'écran
            '
            'Dim att As System.Net.Mail.Attachment
            ''
            mail.From = AppManager.SupportFROM
            mail.To = AppManager.SupportTO
            'mail.Cc = MetaValue.GetMetaValueByCode("OPT_PAYMASTER_EMAIL").Value
            mail.Subject = String.Format("IT-SUPP-CASE : Support EMAIL from {0}.{1} ({2})", AppManager.DbServerName, AppManager.DbName, AppManager.UserName)
            mail.Body = "Date : " + Date.Now.ToString("yyyy/MM/dd HH:mm") + "<BR>" +
                        "UserName : " + AppManager.UserName + "<BR>" +
                        "ServerName : " + AppManager.DbServerName + "<BR>" +
                        "DBName : " + AppManager.DbName + "<BR>" + "<BR>" +
                        "Comment :" + "<BR>" +
                        Me.meComment.Text.Replace(vbCrLf, "<BR>") + "<BR><BR>" +
                        "<b><font color=red>ERROR : </font></b>" + "<BR>" +
                        Me.Memo.Text.Replace(vbCrLf, "<BR>")

            mail.Message.IsBodyHtml = True

            If dataStream IsNot Nothing Then
                'mail.AddAttachment(DirectoryA)
                dataStream.Position = 0

                '
                ' Mettre en commentaire pour enlever le print screen de l'écran
                '
                'att = New System.Net.Mail.Attachment(dataStream, "ERROR.jpg", "image/jpeg")
                'mail.Message.Attachments.Add(att)
            End If
            mail.SendMessage()

        Catch ex As System.Exception
            MsgBox(ex.Message)

        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    ''' <summary>
    ''' Captureur d'écran...
    ''' </summary>
    ''' <remarks>Serait important de le mettre dans le FRAMEWORK</remarks>
    Private Shared Sub CaptureScreen()
        Try
            'OUTSTANDINGISSUE : Serait important de le mettre dans le FRAMEWORK
            Using BMP As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim ScreenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
                dataStream = New System.IO.MemoryStream

                g.CopyFromScreen(New Point(0, 0), New Point(0, 0), ScreenSize)
                BMP.Save(dataStream, Imaging.ImageFormat.Jpeg)
            End Using
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Class